import { query } from '../../config/db.js';
import logger from '../../config/logger.js';

// 从seed_new_students.js获取的班级信息
const classes = [
  '395', '396', '397', '398', '399'
];

// 从seed_new_students.js获取的学生姓名（部分）
const studentNames = [
  '王伟', '李强', '张伟', '刘军', '陈勇', '杨杰', '赵涛', '黄磊', '周磊', '吴超',
  '徐明', '孙刚', '胡辉', '朱强', '高军', '林杰', '何浩', '郭轩', '马博', '罗宇',
  '王芳', '李娜'
];

// 从seed_clubs.js获取的社团名称
const clubNames = [
  '篮球社', '足球社', '乒乓球社', '羽毛球社', '网球社',
  '音乐社', '舞蹈社', '戏剧社', '摄影社', '绘画社',
  '书法社', '文学社', '科技社', '机器人社', '航模社',
  '环保社', '志愿者社', '辩论社', '演讲社', '象棋社',
  '围棋社', '动漫社'
];

// 从seed_clubs.js获取的社团位置
const clubLocations = [
  '体育馆1楼', '体育馆2楼', '艺术楼101室', '艺术楼202室',
  '科技楼303室', '科技楼404室', '图书馆1楼', '图书馆2楼',
  '教学楼B栋101室', '教学楼B栋202室', '教学楼C栋303室'
];

// 生成随机备注
function generateRemark() {
  const remarks = ['积极报名', '对社团有浓厚兴趣', '有相关经验', '希望学习新知识', ''];
  return remarks[Math.floor(Math.random() * remarks.length)];
}

async function seedClubRegistrations() {
  try {
    logger.info('开始播种社团报名数据...');

    // 先清空社团报名表
    // 禁用外键约束
    await query('SET FOREIGN_KEY_CHECKS = 0');
    await query('TRUNCATE TABLE club_registrations');
    // 启用外键约束
    await query('SET FOREIGN_KEY_CHECKS = 1');
    // 重置自增序列
    await query('ALTER TABLE club_registrations AUTO_INCREMENT = 1');
    logger.info('社团报名表已清空');

    // 生成22条社团报名数据
    const registrations = [];
    for (let i = 0; i < 22; i++) {
      const className = classes[Math.floor(Math.random() * classes.length)];
      const studentName = studentNames[i]; // 确保学生姓名不重复
      const clubName = clubNames[Math.floor(Math.random() * clubNames.length)];
      const clubLocation = clubLocations[Math.floor(Math.random() * clubLocations.length)];
      const remark = generateRemark();

      registrations.push({
        class_name: className,
        student_name: studentName,
        club_name: clubName,
        club_location: clubLocation,
        remarks: remark
      });
    }

    // 插入社团报名数据
    for (const registration of registrations) {
      await query(
        'INSERT INTO club_registrations (class_name, student_name, club_name, club_location, remarks) VALUES (?, ?, ?, ?, ?)',
        [
          registration.class_name,
          registration.student_name,
          registration.club_name,
          registration.club_location,
          registration.remarks
        ]
      );
    }
    logger.info('已成功插入22条社团报名数据');

    logger.info('社团报名数据播种完成');
  } catch (error) {
    logger.error('社团报名数据播种失败:', error);
    throw error;
  }
}

export default seedClubRegistrations;